package com.Tadama.Forms;

import com.Tadama.MIDlet.TadamaMIDlet;
import com.Tadama.Utility.CommandBuilder;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

public class frmWaitScreem extends WaitScreen implements CommandListener{
    
    private SimpleCancellableTask simpleCancellableTask;
    private static final Command cmOk = CommandBuilder.getOk();
    private TadamaMIDlet mainMidlet;
    private Alert alerta;
    private int caso;
    private  Displayable previo;
            
    public frmWaitScreem(Display display,TadamaMIDlet midlet,Displayable displayPrevio,int caso) throws IllegalArgumentException {
        super(display);
        this.mainMidlet=midlet;
        this.caso= caso;
        this.previo=displayPrevio;
        init();
    }
    
    private void init(){
        this.addCommand(cmOk);
        this.setTitle("Wait Screen");
        this.setCommandListener(this);
        this.setImage(loadImage("/com/Tadama/img/sandglass.png"));
        this.setText("Please Wait...");
        this.setTask(getSimpleCancellableTask());
    }
    
    public SimpleCancellableTask getSimpleCancellableTask() {
        if (simpleCancellableTask == null) {
            simpleCancellableTask = new SimpleCancellableTask();
            simpleCancellableTask.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {
                }
            }
                    );
        }
        return simpleCancellableTask;
    }
    private static Image loadImage(String name) {
        Image image = null;
        try {
            image = Image.createImage(name);
        }catch (IOException ioe){
            System.out.println(ioe);
        }
        return image;
    }
    
    public Alert getAlert(String titulo,String texto) {
        if (alerta == null) {
            alerta = new Alert(titulo, texto, null, AlertType.INFO);
            alerta.addCommand(cmOk);
            alerta.setCommandListener(this);
            alerta.setTimeout(Alert.FOREVER);
        }
        return alerta;
    }
    public void commandAction(Command c, Displayable d) {
        if(d== this){
            if(caso ==1){
                if (c == WaitScreen.FAILURE_COMMAND) {
                    Display.getDisplay(mainMidlet).setCurrent(new frmPedido(mainMidlet,this));
                } else if (c == WaitScreen.SUCCESS_COMMAND) {     
                    Display.getDisplay(mainMidlet).setCurrent(getAlert("Busqueda Clientes"," Filtrado de Clientes Completada"), new lstClientes(mainMidlet,"Lista de Clientes",List.EXCLUSIVE,this));
                }
            }
            if(caso ==2){
                if (c == WaitScreen.FAILURE_COMMAND) {
                    Display.getDisplay(mainMidlet).setCurrent(new frmPedido(mainMidlet,this));
                } else if (c == WaitScreen.SUCCESS_COMMAND) {     
                    Display.getDisplay(mainMidlet).setCurrent(getAlert("Busqueda Productos"," Filtrado de Productos Completada"), new lstPedido(mainMidlet,"Pedidos",List.MULTIPLE,this));
                }
            }
            if(caso ==3){
                if (c == WaitScreen.FAILURE_COMMAND) {
                    Display.getDisplay(mainMidlet).setCurrent(new frmPedido(mainMidlet,this));
                } else if (c == WaitScreen.SUCCESS_COMMAND) {     
                    Display.getDisplay(mainMidlet).setCurrent(getAlert("Busqueda Pedidos"," Filtrado de Pedidos Completada"), new lstPedido(mainMidlet,"Devoluciones",List.IMPLICIT,this));
                }
            }
        }
        if(d==alerta && caso ==1)
        {
            if(c==cmOk)
                Display.getDisplay(mainMidlet).setCurrent(new lstClientes(mainMidlet,"Clientes",List.EXCLUSIVE,previo));
        }
        if(d==alerta && caso ==2)
        {
            if(c==cmOk)
                Display.getDisplay(mainMidlet).setCurrent(new lstProductos(mainMidlet,"Productos",List.MULTIPLE,previo));
        }
        if(d==alerta && caso ==3)
        {
            if(c==cmOk)
                Display.getDisplay(mainMidlet).setCurrent(new lstPedido(mainMidlet,"Pedido",List.EXCLUSIVE,previo));
        }
    }   
}